package cn.datawin.common.state.template;

import cn.datawin.common.state.StateMachineUtil;
import cn.datawin.common.state.action.StateWindUpAction;
import cn.datawin.common.state.constant.State;
import cn.hutool.core.util.StrUtil;
import org.squirrelframework.foundation.fsm.UntypedAnonymousAction;
import org.squirrelframework.foundation.fsm.UntypedStateMachineBuilder;

import java.util.List;


/**
 * @Author: huanyayun
 * @Date: 2021/10/27 16:00
 */
public class SimpleTemplate extends BaseTemplate {

    public SimpleTemplate(String stateName, UntypedAnonymousAction action, String entryFunc, String exitFunc) {
        super(stateName, action, entryFunc, exitFunc);
    }
    public SimpleTemplate(String stateName, UntypedAnonymousAction action) {
        super(stateName, action, "onEntry","onExit");
    }

    public SimpleTemplate(String stateName, List<UntypedAnonymousAction> actions, String entryFunc, String exitFunc) {
        super(stateName, actions, entryFunc, exitFunc);
    }


    @Override
    public void execActions(UntypedStateMachineBuilder builder) {
        //定义执行进入外部状态stateName的方法
        if(StrUtil.isNotBlank(entryFunc)){
            StateMachineUtil.onEntryCallMethod(stateName,builder,entryFunc);
        }
        //进入状态执行参数中actions外加CheckEndAction,检测是否触发完毕
        actions.add(new StateWindUpAction());
        //定义进入外呼状态stateName的执行的actions
        StateMachineUtil.onEntry(stateName,builder,actions);
        //定义外部状态stateName到end状态的链路
        StateMachineUtil.external(stateName, State.END,builder);
        //定义执行离开外部状态stateName的方法
        if(StrUtil.isNotBlank(exitFunc)){
            StateMachineUtil.onExitCallMethod(stateName,builder,exitFunc);
        }

    }
}
